package school.valon.business.impl;

import org.springframework.transaction.annotation.Transactional;

import school.valon.business.SchoolPolicyBusiness;
import school.valon.business.base.BaseBusiness;
import school.valon.dto.SchoolPolicyDto;
import school.valon.dto.StudentDto;
import school.valon.persistence.dao.SchoolDao;
import school.valon.persistence.dao.StaffDao;
import school.valon.persistence.pojo.SchoolPolicy;
import school.valon.persistence.pojo.Staff;
import school.valon.persistence.pojo.Student;

@Transactional
public class SchoolPolicyBusinessImpl extends BaseBusiness implements SchoolPolicyBusiness {

	private SchoolDao schoolDao;
	private StaffDao staffDao;

	public void setSchoolDao(SchoolDao schoolDao) {
		this.schoolDao = schoolDao;
	}

	public void setStaffDao(StaffDao staffDao) {
		this.staffDao = staffDao;
	}

	public SchoolPolicyDto saveSchoolPolicy(SchoolPolicyDto schoolPolicyDto) {
		SchoolPolicy schoolPolicy = null;
		if (schoolPolicyDto.getPolicyId() == null) {
			Staff staff = null;
			if (schoolPolicyDto.getStaff() != null && schoolPolicyDto.getStaff().getStaffId() != null) {
				staff = staffDao.find(schoolPolicyDto.getStaff().getStaffId());
			}
			schoolPolicy = mapper.map(schoolPolicyDto, SchoolPolicy.class);
			schoolPolicy.setStaff(staff);
			schoolPolicy = schoolDao.createSchoolPolicy(schoolPolicy);
		} else {
			schoolPolicy = schoolDao.findSchoolPolicy(schoolPolicyDto.getPolicyId());
			schoolPolicy.setCreatedOn(schoolPolicyDto.getCreatedOn());
			schoolPolicy.setItem(schoolPolicyDto.getItem());
			schoolPolicy.setItemDetails(schoolPolicyDto.getItemDetails());
			schoolDao.updateSchoolPolicy(schoolPolicy);
		}

		SchoolPolicyDto newSchoolPolicyDto = mapper.map(schoolPolicy, SchoolPolicyDto.class);
		newSchoolPolicyDto.setStaff(null);
		return newSchoolPolicyDto;
	}

	public void deleteSchoolPolicy(Integer schoolPolicyId) {
		schoolDao.deleteSchoolPolicy(schoolPolicyId);
	}

	@Transactional(readOnly=true)
	public StudentDto getStudentReportPerformance(Integer studentId) {
		Student student = null;
		if (studentId != null) {
			//student = studentDao.find(studentId);
			return null;
		}
		return null;
	}
}
